//
//  QuestionCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/23.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class QuestionCell: UITableViewCell {
    
    var model : QuestionModel! {
        didSet{
            getData()
        }
    }
    
    fileprivate let newsImageView = UIImageView()
    fileprivate let titleLabel = UILabel()
    fileprivate let contextLabel = UILabel()
    fileprivate let timeLabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.addSubview(newsImageView)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.contentView.addSubview(titleLabel)
        
        contextLabel.textColor = UIColor.lightGray
        contextLabel.numberOfLines = 2
        contextLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(contextLabel)
        
        timeLabel.textAlignment = .right
        timeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(timeLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView)
        
        newsImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.width.equalTo(RATIO(maxNum: 120))
            make.height.equalTo(RATIO_H(maxNum: 100))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(newsImageView)
            make.left.equalTo(newsImageView.snp.right).offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        contextLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.right.equalTo(titleLabel)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(contextLabel)
            make.right.equalTo(contextLabel)
            make.top.equalTo(contextLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 1))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 加载数据
    fileprivate func getData() {
        
        newsImageView.setUrlImage(with: kmHttps + "/data/upload/" + (model.thumb ?? ""), placeholder: UIImage.imageWithName("loading"))
        titleLabel.text = model.posts_title
        contextLabel.text = model.posts_excerpt
        timeLabel.text = model.update_time?.getDate()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}
